// FindReplaceDlg.cpp: implementation of the CFindReplaceDlg class.
//
//////////////////////////////////////////////////////////////////////

#include "FindReplaceDlg.h"
#include "EditWindow.h"
#include "../WndReg.h"

extern CWndReg reg;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFindReplaceDlg::CFindReplaceDlg(IFindReplaceHost* host, bool bReplace/*=false*/)
{
    assert (host);
    m_bReplaceDlg = bReplace;
    m_host = host;
}

CFindReplaceDlg::~CFindReplaceDlg()
{
}

void CFindReplaceDlg::SetAsReplaceDlg(bool bReplace/*=true*/)
{
    assert (!IsWindow());
    m_bReplaceDlg = bReplace;
}

LRESULT CFindReplaceDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    HWND hCtl;
    reg.AddWnd(m_hWnd);
    if (!m_bReplaceDlg) {
        hCtl = GetDlgItem(IDC_STATIC_REPLACE);
        ::EnableWindow(hCtl, FALSE);

        hCtl = GetDlgItem(IDC_REPLACETEXT);
        ::EnableWindow(hCtl, FALSE);
        
        hCtl = GetDlgItem(IDC_REPLACE);
        ::EnableWindow(hCtl, FALSE);

        hCtl = GetDlgItem(IDC_REPLACEALL);
        ::EnableWindow(hCtl, FALSE);
    } 
    CheckRadioButton(IDC_RADIOUP, IDC_RADIODOWN, IDC_RADIODOWN);
    ShowWindow(SW_SHOW);
    hCtl = GetDlgItem(IDC_FIND);
    ::SetFocus(hCtl);
    bHandled = TRUE;
    return TRUE;
}

LRESULT CFindReplaceDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    reg.RemoveWnd(m_hWnd);
    return 0;
}

LRESULT CFindReplaceDlg::OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    switch (wParam) {
    case 0x0d: /* return key*/
        ::PostMessage(m_hWnd, WM_COMMAND, IDC_NEXT, 0);
        break;
        
    case 0x09: /* tab key*/
        if (GetKeyState(VK_SHIFT) < 0 ) {
            /* SHIFT-TAB*/
            PrevDlgCtrl();
        } else {
            NextDlgCtrl();
        }

        break;
    }

    return 0;
}

LRESULT CFindReplaceDlg::OnNcDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    assert (m_host);
    m_host->FindReplaceDlgDestroyed();
    bHandled = FALSE;
    return 0;
}

LRESULT CFindReplaceDlg::OnFindNext(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    char* str;
    HWND hEdit = GetDlgItem(IDC_FIND);
    assert (hEdit);

    int len = ::GetWindowTextLength(hEdit);
    if (!len) {
        return 0;
    }

    str = (char*)malloc ((size_t)len + 1);
    if (!str) {
        return 0;
    }
    bool up = (IsDlgButtonChecked(IDC_RADIOUP) ? true : false);
    ::GetWindowText(hEdit, str, len+1);
    assert (m_host);
    m_host->FindNext(str, up);
    free (str);

    if (!m_bReplaceDlg) {
        DestroyWindow();
    }
    return 0;
}

LRESULT CFindReplaceDlg::OnReplace(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    assert (m_bReplaceDlg);
    char *str0, *str1;
    HWND hFind = GetDlgItem(IDC_FIND);
    HWND hReplaceText = GetDlgItem(IDC_REPLACETEXT);

    assert (hFind);

    int len = ::GetWindowTextLength(hFind);
    if (!len) {
        return 0;
    }

    str0 = (char*)malloc ((size_t)len + 1);
    if (!str0) {
        return 0;
    }
    ::GetWindowText(hFind, str0, len+1);

    len = ::GetWindowTextLength(hReplaceText);
    str1 = (char*)malloc ((size_t)len + 1);
    if (!str1) {
        free (str0);
        return 0;
    }
    ::GetWindowText(hReplaceText, str1, len+1);

    bool up = (IsDlgButtonChecked(IDC_RADIOUP) ? true : false);
    assert (m_host);
    assert (wID == IDC_REPLACE || wID == IDC_REPLACEALL);
    if (wID == IDC_REPLACE) {
        m_host->Replace(str0, str1, up);
    } else {
        m_host->ReplaceAll(str0, str1);
    }
    free (str0);
    free (str1);
    return 0;
}

LRESULT CFindReplaceDlg::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    DestroyWindow();
    return 0;
}

